package com.example.kafkajavaopetion;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

/**
 * @ClassName kafkaConsumerOps
 * @Description TODO
 * @Author zhangxidong
 * @Date 2020/11/24
 * @Version 1.0
 */
public class kafkaConsumerOps {

	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		InputStream in = kafkaConsumerOps.class.getClassLoader().getResourceAsStream("consumer.properties");
		properties.load(in);
		Consumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
		Collection<String> topics = Arrays.asList("hadoop");
		consumer.subscribe(topics);
		ConsumerRecords<String, String> consumerRecords = null;
		while (true) {
			consumerRecords = consumer.poll(1000);
			for (ConsumerRecord consumerRecord : consumerRecords) {
				long offset = consumerRecord.offset();
				int partition = consumerRecord.partition();
				Object key = consumerRecord.key();
				Object value = consumerRecord.value();
				System.out.format("%d\t%d\t%s\t%s\33[32;4m\n", offset, partition, key, value);
			}

		}
	}
}
